package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.wxvo.WxSearchIndexVO;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class WxKeywordServiceImpl implements WxKeywordService {
    @Autowired
    KeywordMapper keywordMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public WxSearchIndexVO searchIndex(Integer userId) {
        KeywordExample keywordExample = new KeywordExample();

        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);

//        排序的规则
        keywordExample.setOrderByClause("update_time"+ " " + "desc");

        List<Keyword> keywordList = keywordMapper.selectByExample(keywordExample);
//        从一个list里面取出第一个对象 下标为0的位置
        Keyword defaultKeyword = keywordList.get(0);


//        查询历史纪录表中的keyword显示出来
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
//        这个地方要去掉显示已经被逻辑删除的 不然一会清空了历史还是可以显示
        SearchHistoryExample.Criteria criteria1 = searchHistoryExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andUserIdEqualTo(userId);
//        排序的规则
        searchHistoryExample.setOrderByClause("update_time" + " " + "desc");
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        List<SingleKeyword> historyKeywordList = new ArrayList<>();

//        怎么把一个集合里面的对象（字段多的）中的放到另外一个集合的对象（字段少）中
        for (SearchHistory searchHistory : searchHistories) {
            //这一步是关键 往另外一个集合里面放的是对象 通过set方法给对象赋值
            SingleKeyword singleKeyword1 = new SingleKeyword();
            singleKeyword1.setKeyword(searchHistory.getKeyword());
            historyKeywordList.add(singleKeyword1);
        }

        KeywordExample keywordExample2 = new KeywordExample();
        KeywordExample.Criteria criteria2 = keywordExample2.createCriteria();
        criteria2.andIsHotEqualTo(true);
        List<Keyword> hotKeywordList = keywordMapper.selectByExample(keywordExample2);

        WxSearchIndexVO wxSearchIndexVO = new WxSearchIndexVO();
        wxSearchIndexVO.setDefaultKeyword(defaultKeyword);
        wxSearchIndexVO.setHistoryKeywordList(historyKeywordList);
        wxSearchIndexVO.setHotKeywordList(hotKeywordList);

        return wxSearchIndexVO;

    }

    @Override
    public String[] searchHelper(String keyword) {
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        if (keyword != null && keyword != "") {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
//        把集合里面的数据放到数组里面
        int i=0;
        String[] data = new String[keywords.size()];
        for (Keyword keyword1 : keywords) {
            data[i++]=keyword1.getKeyword();
        }
        return data;
    }

    @Override
    public void SearchClearHistory(Integer userId) {
//        怎么拿到所有的SearchHistory
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
//        怎么凭空把数据库里面所有的字段全部删掉
//        创造一个只有id的SearchHistory
        ArrayList<SearchHistory> idList = new ArrayList<>();
        for (SearchHistory searchHistory : searchHistories) {
//            new一个新的对象
//            然后把这个对象的Id设置为searchHistory的Id 并且把他的Delete位置设置为true
            SearchHistory searchHistory1 = new SearchHistory();
            searchHistory1.setId(searchHistory.getId());
            searchHistory1.setDeleted(true);
//            最后把对象添加到集合中
            idList.add(searchHistory1);
        }
        for (SearchHistory searchHistory : idList) {
//            遍历我们创造的新的集合并且按照主键只修改delete位置
            searchHistoryMapper.updateByPrimaryKeySelective(searchHistory);
        }

    }
}
